package apj.demo.table.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import apj.demo.factory.ServiceDaoFactory;
import apj.demo.model.Vendor;

/**
 * 供货商操作控制类
 */
@WebServlet("/do.vendor")
public class VendorAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(method != null){
			switch(method){
				case "0":
					try {
						show(request,response);
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				case "1":
					try {
						update_show(request,response);
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				case "2":
					try {
						add_show(request,response);
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				case "3":
					try {
						update(request,response);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					break;
				case "4":
					try {
						add(request,response);
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				default:
					break;
			}
		}
	}

	/*数据显示*/
	private void show(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException{
		String indexStr = request.getParameter("index");
		int index = Integer.parseInt(indexStr);
		List<Vendor> vendors = (ArrayList<Vendor>)ServiceDaoFactory.getVendorServiceDaoImpl().pageData(index);
		com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
		json.put("vendors", vendors);
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
	}

	/*添加供货商操作*/
	private void add(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		//获取数据
		String id = request.getParameter("id");
		String vendor = request.getParameter("vendor");
		String contact_person = request.getParameter("contact_person");
		String vendor_phone = request.getParameter("vendor_phone");
		String vendor_address = request.getParameter("vendor_address");
		String vendor_fax = request.getParameter("vendor_fax");
		
		//数据封装
		Vendor vendorObj = new Vendor();
		vendorObj.setVendor_id(Integer.parseInt(id));
		vendorObj.setVendor(vendor);
		vendorObj.setContact_person(contact_person);
		vendorObj.setVendor_address(vendor_address);
		vendorObj.setVendor_fax(vendor_fax);
		vendorObj.setVendor_phone(vendor_phone);
		ServiceDaoFactory.getVendorServiceDaoImpl().save(vendorObj);
	}

	/*更新供货商操作*/
	private void update(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		//获取数据
		String id = request.getParameter("id");
		String vendor = request.getParameter("vendor");
		String contact_person = request.getParameter("contact_person");
		String vendor_phone = request.getParameter("vendor_phone");
		String vendor_address = request.getParameter("vendor_address");
		String vendor_fax = request.getParameter("vendor_fax");
		
		//数据封装
		Vendor vendorObj = new Vendor();
		vendorObj.setVendor_id(Integer.parseInt(id));
		vendorObj.setVendor(vendor);
		vendorObj.setContact_person(contact_person);
		vendorObj.setVendor_address(vendor_address);
		vendorObj.setVendor_fax(vendor_fax);
		vendorObj.setVendor_phone(vendor_phone);
		ServiceDaoFactory.getVendorServiceDaoImpl().update(vendorObj);
	
	}

	/*添加供货商页面显示*/
	private void add_show(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException{
		
		//获取账单的最大编号
		int maxId = 0;
		ArrayList<Vendor> vendors = (ArrayList<Vendor>)ServiceDaoFactory.getVendorServiceDaoImpl().findByAll();
		if(vendors != null){
			for(Vendor i:vendors){
				if(i.getVendor_id()>maxId){
					maxId = i.getVendor_id();
				}
			}
		}
		
		//数据存入JSON中
		com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
		json.put("maxId", maxId);
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
	
	}

	/*更新供货商数据页面显示*/
	private void update_show(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException{
		String id = request.getParameter("id");
		Vendor update_vendor = ServiceDaoFactory.getVendorServiceDaoImpl().findById(Integer.parseInt(id));
		
		com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
		json.put("update_vendor", update_vendor);
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
	}

}
